using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TesteIATcc.Comuns;

namespace TesteIATcc.Universo
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Fundo : Microsoft.Xna.Framework.GameComponent
    {
        private Sprite spriteFundo1;
        private Sprite spriteFundo2;

        private float timerScroll = 0f;
        private float intervaloScroll = 0.12f;

        public Fundo(TesteIA game, string caminhoImagemFundo)
            : base(game)
        {
            this.spriteFundo1 = new Sprite(game, caminhoImagemFundo, new Vector2(800f, 520f), 0);
            base.Game.Components.Add(this.spriteFundo1);
            this.spriteFundo2 = new Sprite(game, caminhoImagemFundo, new Vector2(800f, 520f), 0);
            base.Game.Components.Add(this.spriteFundo2);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            this.spriteFundo1.PosicaoSprite = new Point(0, 0);
            this.spriteFundo2.PosicaoSprite = new Point((int)this.spriteFundo1.TamanhoSprite.X + this.spriteFundo1.PosicaoSprite.X, 0);

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            this.timerScroll += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (this.timerScroll > this.intervaloScroll)
            {
                if (this.spriteFundo1.PosicaoSprite.X >= (-this.spriteFundo1.TamanhoSprite.X))
                {
                    this.spriteFundo1.PosicaoSprite = new Point(this.spriteFundo1.PosicaoSprite.X - 1, 0);
                }
                else
                {
                    this.spriteFundo1.PosicaoSprite = new Point((int)this.spriteFundo2.TamanhoSprite.X + this.spriteFundo2.PosicaoSprite.X, 0);
                }

                if (this.spriteFundo2.PosicaoSprite.X >= (-this.spriteFundo2.TamanhoSprite.X))
                {
                    this.spriteFundo2.PosicaoSprite = new Point(this.spriteFundo2.PosicaoSprite.X - 1, 0);
                }
                else
                {
                    this.spriteFundo2.PosicaoSprite = new Point((int)this.spriteFundo1.TamanhoSprite.X + this.spriteFundo1.PosicaoSprite.X, 0);
                }

                this.timerScroll = 0f;
            }

            base.Update(gameTime);
        }
    }
}